package org.gpslw.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.geotools.api.feature.simple.SimpleFeature;
import org.geotools.api.feature.type.PropertyDescriptor;
import org.springframework.boot.jackson.JsonComponent;


import java.io.IOException;
import java.util.Collection;

@JsonComponent
public class SimpleFeatureSerilizer extends JsonSerializer<SimpleFeature> {
    @Override
    public void serialize(SimpleFeature simpleFeature, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        Collection<PropertyDescriptor> descriptors = simpleFeature.getFeatureType().getDescriptors();
        jsonGenerator.writeStartObject();
        for (PropertyDescriptor descriptor : descriptors){
            jsonGenerator.writeObjectField(descriptor.getName().getLocalPart(), simpleFeature.getAttribute(descriptor.getName()));
        }
        jsonGenerator.writeEndObject();
    }

    @Override
    public Class<SimpleFeature> handledType() {
        return SimpleFeature.class;
    }

}
